package com.zzuli.yxy.task;

import com.zzuli.yxy.entity.Singer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.zzuli.yxy.service.SingerService;
import com.zzuli.yxy.task.springDataPipeline.SingerImgPipeline;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.BloomFilterDuplicateRemover;
import us.codecraft.webmagic.scheduler.QueueScheduler;

import java.util.List;

/**
 * 目标地址：https://music.163.com/artist/desc?id=3684
 * 获取 歌手图片、歌手简介
 * @auther pony
 * @create 2023-04-05 13:30
 */
@Component
public class SingerImgTask implements PageProcessor {

    @Autowired
    private SingerService singerService;

    private String basicUrl = "https://music.163.com/artist/desc?id=";


    @Override
    public void process(Page page) {
        //用于封装 歌手id、歌手图片、歌手简介信息
        Singer singer = new Singer();
        //1、根据路径，获取歌手id
        String url = page.getUrl().toString();
        System.out.println("url:"+url);
        // 截取获得 songId
        String songId = url.substring(url.lastIndexOf("=") + 1);
        System.out.println("songId:"+songId);
        //2、获取歌手图片地址
        // 转化为string <img src="http://p2.music.126.net/7636PzUiFMETHU7SAr05FA==/109951167878710661.jpg?param=640y300">
        String singerImg = page.getHtml().css("div.g-wrap6 div.n-artist img").toString();
        System.out.println("singerHtmlImg:" + singerImg);
        // 截取获得地址
        String singerImgUrl = singerImg.substring(singerImg.indexOf("src=\"") + 5, singerImg.lastIndexOf("\""));
        System.out.println("singerImgUrl:" + singerImgUrl);

        //3、根据路径，获取歌手简介
        String singerRemarks  = page.getHtml().css("div.n-artdesc p", "text").toString();
        System.out.println("singerRemarksString:"+singerRemarks);

        //4、封装数据
        singer.setSingerId(Integer.valueOf(songId));
        singer.setSingerImg(singerImgUrl);
        singer.setSingerRemarks(singerRemarks);

        //5、保存数据给 Pipeline 进行数据库保存
        page.putField("singer", singer);
    }

    private Site site = Site.me();
    @Override
    public Site getSite(){
        return site;
    }

    @Autowired
    private SingerImgPipeline singerImgPipeline;
//    @Scheduled(initialDelay = 1 * 1000, fixedRate=1000*60*60*24*7)
    public void process(){
        //1、查询数据库，获取所有歌手
        List<Singer> singerlist = singerService.list();
        //2、遍历 songList，得到每个歌手的 id
        for (Singer singer : singerlist) {
            // 3、通过id，拼接要爬虫地址
            Integer singerId = singer.getSingerId();
            String url = basicUrl + singerId;
            // 4、配置请求信息
            Request request = new Request(url);
            // 请求方法
            request.setMethod("GET");
            // 浏览器基本信息，用于反爬
            request.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36");

            Spider.create(new SingerImgTask())
                    .addRequest(request)
                    // 使用布隆过滤器进行去重， 10000 大概要放的数据，数据越大越慢
                    // 使用前先引一个新版本的依赖
                    .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(10000)))
                    .thread(10)
                    .addPipeline(this.singerImgPipeline)
                    .run();
        }
    }
}
